package com.zhangh.summer.admin.support.boot.common;

import com.zhangh.summer.admin.support.boot.interfaces.OnAppConfigConditional;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

import java.util.Map;

/**
 * @description: TODO
 * @author: zhanghui  zhanghlucky@163.com
 * @create: 2020-12-04 21:56
 **/
public class AppConfigCondition implements Condition {
    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        Map<String, Object> annotationAttributes = annotatedTypeMetadata.getAnnotationAttributes(OnAppConfigConditional.class.getName());
        AppConfigEnum configEnum = (AppConfigEnum) annotationAttributes.get("value");

        if (configEnum.getCode().equals(AppConfigEnum.MYSQL_DATASOURCE_ENABLE.getCode())){
            if (ApplicationConfig.config().isMysqlDataSourceEnable()){
                return true;
            }
        }

        if (configEnum.getCode().equals(AppConfigEnum.WEBMVC_CONFIGURATION_ENABLE.getCode())){
            if (ApplicationConfig.config().isWebMvcConfigurationEnable()){
                return true;
            }
        }

        if (configEnum.getCode().equals(AppConfigEnum.CORS_ENABLE.getCode())){
            if (ApplicationConfig.config().isCorsEnable()){
                return true;
            }
        }

        if (configEnum.getCode().equals(AppConfigEnum.REDIS_ENABLE.getCode())){
            if (ApplicationConfig.config().isRedisEnable()){
                return true;
            }
        }
        if (configEnum.getCode().equals(AppConfigEnum.JOB_ENABLE.getCode())){
            if (ApplicationConfig.config().isJobEnable()){
                return true;
            }
        }

        if (configEnum.getCode().equals(AppConfigEnum.NONE.getCode())){
            return true;
        }

        return false;
    }
}
